﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToVis.CApExportToVisManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToVis;

public class CApExportToVisManager
{
  private CApC3DSnapshotApp m_snapshotApp = new CApC3DSnapshotApp();
  private CApC3DSimulationApp m_simulationApp = new CApC3DSimulationApp();
  private CApC3DDocumentationSettings m_documentationSettings;
  private CApC3DDataManagerApp m_dataManager = new CApC3DDataManagerApp();
  private Hashtable m_fileNames = new Hashtable();
  public string m_AnimationFramesFileName;
  private CApExportToVisViewModel _apVM;

  public CApExportToVisManager(CApExportToVisViewModel _apVM)
  {
    this.m_dataManager.Initialize();
    this.m_documentationSettings = this.m_dataManager.GetDocumentationSettings();
    this._apVM = _apVM;
  }

  public bool GenerateRoot()
  {
    bool root = true;
    CApC3DRootNodeData capC3DrootNodeData = new CApC3DRootNodeData(this._apVM.Operation);
    try
    {
      this.m_snapshotApp.CreateCurrentSnapshot();
      capC3DrootNodeData.AttachedSnapshotData = this.m_snapshotApp.CurrentSnapshot;
      string path1 = "C:\\Temp";
      CApC3DAttachmentNameGenerator dattachmentNameGenerator = new CApC3DAttachmentNameGenerator();
      string path2_1 = "view.js";
      if (capC3DrootNodeData.PlanningNodeObject != null)
        path2_1 = dattachmentNameGenerator.GenerateAnimationFileName(capC3DrootNodeData.PlanningNodeObject, CApC3DUtils.JSExtension, false);
      string path2_2 = "frames_" + path2_1;
      string fileName = Path.Combine(path1, path2_1);
      string animationJSFileName = Path.Combine(path1, path2_2);
      CApExportToVisUtilities.GetAllDisplayedObjects();
      CApC3DAppearanceSet appearanceSet = new CApC3DAppearanceSet();
      this.m_simulationApp.ExportData = new TxExportGraphicsAnimationData()
      {
        JsonFiles = this.m_fileNames
      };
      this.m_simulationApp.Format = (TxExportGraphicsAnimationFileFormatEx) 3;
      this.m_simulationApp.ExportSimulation = true;
      this.m_simulationApp.Initialize((CApC3DProcessNodeData) capC3DrootNodeData, fileName, animationJSFileName, this.m_documentationSettings, appearanceSet);
      this.m_AnimationFramesFileName = animationJSFileName;
      this.m_simulationApp.StartStepRecording += new CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventHandler(this.m_simulationApp_StartStepRecording);
      capC3DrootNodeData.IsStep = true;
      root = this.RecordObject((CApC3DProcessNodeData) capC3DrootNodeData);
      TxExportGraphicsAnimationEx.GetAnimationOutputData();
      this.m_simulationApp.StartStepRecording -= new CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventHandler(this.m_simulationApp_StartStepRecording);
      this.m_simulationApp.Terminate();
    }
    catch (Exception ex)
    {
      throw;
    }
    finally
    {
      this.m_snapshotApp.RestoreCurrentSnapshot(false);
      this.m_snapshotApp.DeleteTempSnapshots((CApC3DProcessNodeData) capC3DrootNodeData);
    }
    return root;
  }

  private bool RecordObject(CApC3DProcessNodeData operation)
  {
    bool flag = true;
    if (operation.IsStep)
    {
      this.ApplyFullSnapshot(operation.AttachedSnapshotData.Snapshot);
      flag = this.m_simulationApp.Record(operation);
    }
    if (flag)
    {
      List<CApC3DProcessNodeData> children = operation.GetChildren(true);
      int count = children.Count;
      for (int index = 0; index < count & flag; ++index)
        flag = this.RecordObject(children[index]);
    }
    return flag;
  }

  private void ApplyFullSnapshot(TxSnapshot snapshot)
  {
    if (!TxSnapshot.op_Inequality(snapshot, (ITxObject) null))
      return;
    snapshot.Apply(new TxApplySnapshotParams()
    {
      ObjectsLocation = true,
      ObjectsVisibility = true,
      PointOfView = true,
      DevicePoses = true,
      ObjectsAttachments = true,
      ObjectsColor = true,
      ObjectsViewingMode = true,
      PMIsScale = true
    });
  }

  private void m_simulationApp_StartStepRecording(
    object sender,
    CApC3DSimulationApp.CApC3DSimulationApp_StartStepRecordingEventArgs args)
  {
    this.m_snapshotApp.SetSnapshot(args.Step);
  }
}
